package com.shop.controller;

import com.shop.common.SetMeta;
import com.shop.common.Upload;
import com.shop.entity.Good;
import com.shop.exception.GoodException;
import com.shop.service.GoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
public class GoodController {



    @Autowired
    private GoodService goodService;

    /**
     * 商品列表数据
     * @param query
     * @param pagenum
     * @param pagesize
     * @return
     */
    @PreAuthorize("hasAuthority('153')")
    @GetMapping("/goods")
    public SetMeta selectPageGood(@RequestParam(required = false) String query,
                                   @RequestParam(required = false) Integer pagenum,
                                   @RequestParam(required = false) Integer pagesize){
        SetMeta setMeta;
        try {
            setMeta = goodService.selectPageGood(query,pagenum,pagesize);
        } catch (GoodException e) {
            setMeta=new SetMeta();
            setMeta.setError(e.getMessage());
        }
        return setMeta;
    }

    /**
     * 添加商品
     * @param goods
     * @return
     */
    @PreAuthorize("hasAuthority('105')")
    @PostMapping("/goods")
    public SetMeta insertGood(@RequestBody Good goods){
        SetMeta setMeta;
        try{
            setMeta=goodService.insertGood(goods);
        }catch(GoodException e){
            setMeta=new SetMeta();
            setMeta.setError(e.getMessage());
        }

        return setMeta;
    }

    /**
     * 根据id查询商品
     * @param id
     * @return
     */
    @PreAuthorize("hasAuthority('153')")
    @GetMapping("/goods/{id}")
    public SetMeta selectGood(@PathVariable Integer id){
        SetMeta setMeta;
        try {
            setMeta=goodService.selectGood(id);
        } catch (GoodException e) {
            setMeta=new SetMeta();
            setMeta.setError(e.getMessage());
        }
        return setMeta;
    }

    /**
     * 编辑提交商品
     * @param id
     * @param goods
     * @return
     */
    @PreAuthorize("hasAuthority('116')")
    @PutMapping("/goods/{id}")
    public SetMeta updateGood(@PathVariable Integer id,@RequestBody Good goods){
        SetMeta setMeta;
        try {
            goods.setGoods_id(id);
            setMeta=goodService.updateGood(goods);
        } catch (GoodException e) {
            setMeta=new SetMeta();
            setMeta.setError(e.getMessage());
        }
        return setMeta;
    }

    /**
     * 删除商品
     * @param id
     * @return
     */
    @PreAuthorize("hasAuthority('117')")
    @DeleteMapping("/goods/{id}")
    public SetMeta deleteGood(@PathVariable Integer id) {
        SetMeta setMeta = new SetMeta();
        try {
            setMeta = goodService.deleteGood(id);
        } catch (GoodException e) {
            setMeta.setError(e.getMessage());
        }
        return setMeta;
    }

    /**
     * 图片上传
     * @param file
     * @return
     */
    @PreAuthorize("hasAuthority('105')")
    @PostMapping("upload")
    public SetMeta upload(@RequestParam MultipartFile file) {
        SetMeta setMeta = new SetMeta();
        if (file.isEmpty()) {
            setMeta.setError("图片上传失败");
        } else {
            String filename = file.getOriginalFilename();
            String suffixName = filename.substring(filename.lastIndexOf("."));
            String filePath = "D:/tmp_uploads/";
            filename = UUID.randomUUID() + suffixName;
            File dest = new File(filePath + filename);
            if (!dest.getParentFile().exists()) {
                dest.getParentFile().mkdirs();
            }
            try {
                file.transferTo(dest);
            } catch (IOException e) {
                e.printStackTrace();
            }

            Upload upload = new Upload("tmp_uploads/" + filename, "http://127.0.0.1:8888/tmp_uploads/" + filename);
            setMeta.setSuccess(upload,"上传成功");
        }
        return setMeta;
    }

    // 150 更新商品图片
    //@PutMapping("goods/{id}/pics")

    // 151 更新商品属性
    // 152 更新商品状态
}
